package com.bytedance.scenedemo;

import com.bytedance.scene.Scene;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scenedemo.group.viewpager.ViewPagerSample;
import com.bytedance.scenedemo.migrate.*;
import com.bytedance.scenedemo.migrate.migrate_from_classic_ability_fraction.MigrateFromClassicOhosAbilitySamplesAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;

public class MigrateSamples extends Scene implements Component.ClickedListener {
    @Override
    public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component parse = scatter.parse(ResourceTable.Layout_fraction_migrate_samples, container, false);
        Text text = (Text) parse.findComponentById(ResourceTable.Id_text_navigation_list_hint);
        text.setText(getClassName());
        initListener(parse);
        return parse;
    }

    private void initListener(Component parse) {
        Button bindNavigationsceneToFragment = (Button) parse.findComponentById(ResourceTable.Id_button_bind_navigationscene_to_fragment);
        bindNavigationsceneToFragment.setClickedListener(this);

        Button bindGroupsceneToActivity = (Button) parse.findComponentById(ResourceTable.Id_button_bind_groupscene_to_activity);
        bindGroupsceneToActivity.setClickedListener(this);

        Button bindGroupsceneToFragment = (Button) parse.findComponentById(ResourceTable.Id_button_bind_groupscene_to_fragment);
        bindGroupsceneToFragment.setClickedListener(this);

        Button bindToView = (Button) parse.findComponentById(ResourceTable.Id_button_bind_to_view);
        bindToView.setClickedListener(this);

        Button migrateFromClassicApp = (Button) parse.findComponentById(ResourceTable.Id_button_migrate_from_classic_app);
        migrateFromClassicApp.setClickedListener(this);
    }


    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_button_bind_navigationscene_to_fragment:
            case ResourceTable.Id_button_bind_groupscene_to_activity:
            case ResourceTable.Id_button_bind_groupscene_to_fragment:
                NavigationSceneGetter.requireNavigationScene(MigrateSamples.this).push(ViewPagerSample.class);
                break;
            case ResourceTable.Id_button_bind_to_view:
                JumpIntent(TestSceneToViewAbility.class);
                break;
            case ResourceTable.Id_button_migrate_from_classic_app:
                JumpIntent(MigrateFromClassicOhosAbilitySamplesAbility.class);
                break;
            default:
                break;
        }
    }

    private void JumpIntent(Class cls){
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getActivity().getBundleName())
                .withAbilityName(cls)
                .build();
        intent.setOperation(operation);
        SceneExtensions.requireNavigationScene(this).startActivity(intent);
    }
}
